/**
 * \file drv_key.c
 * \brief
 * \version 0.1
 * \date 2021-02-28
 * \author likailong (likailong@skg.com)
 *
 * Copyright (c) 2021 by SKG. All Rights Reserved.
 */

/*Includes ----------------------------------------------*/

#include <string.h>

#include "hal.h"
#include "flexible_button.h"

/**\defgroup DRV_KEY_Private_TypesDefinitions
 * \{
 */

typedef struct
{
    uint32_t Port;
    uint32_t  Pin;
    uint8_t   Level;
} DrvKeyGpio_t;

typedef flex_button_t KeyUnit_t;
#define FLEX2KEY_EVENT(e) (0x0001 << (e))
#define KEY_NUMBER  (24)

/**
 * \}
 */

/**\defgroup DRV_KEY_Private_Defines
 * \{
 */

#ifndef NULL
#define NULL ((void *)0)
#endif

/**
 * \}
 */

/**\defgroup DRV_KEY_Private_Variables
 * \{
 */
const static DrvKeyGpio_t s_KeyGpioTable[KEY_NUMBER];

static KeyUnit_t    s_KeyList[KEY_NUMBER];
//static DrvKeyInfo_t DrvKeyInfo[KEY_NUMBER];

/**
 * \}
 */

/**\addtogroup DRV_KEY_Private_Functions
 * \{
 */
static uint8_t _DrvKeyRead(KeyUnit_t *pBtn)
{
    return GPIO_READ_PIN(s_KeyGpioTable[pBtn->id].Port, s_KeyGpioTable[pBtn->id].Pin);
}

static void _DrvKeyCallback(KeyUnit_t *pKeyInfo)
{
    #if 0
    uint16_t Event = FLEX2KEY_EVENT(pKeyInfo->event);
    if ((Event & DrvKeyInfo[pKeyInfo->id].Event) && (DrvKeyInfo[pKeyInfo->id].pCB != NULL))
    {
        DrvKeyInfo[pKeyInfo->id].pCB(Event, pKeyInfo->click_cnt);
    }
	#endif
}


void DrvKeyInit(uint16_t ShortTime, uint16_t LongTime, uint16_t LongHoldTime)
{
    int i;
	#if 0
    memset(DrvKeyInfo, 0, sizeof(DrvKeyInfo));
    for (i = 0; i < KEY_NUMBER; i++)
    {
        s_KeyList[i].id                     = i;
        s_KeyList[i].pressed_logic_level    = s_KeyGpioTable[i].Level;
        s_KeyList[i].usr_button_read        = _DrvKeyRead;
        s_KeyList[i].cb                     = _DrvKeyCallback;
        s_KeyList[i].short_press_start_tick = FLEX_MS_TO_SCAN_CNT(ShortTime);
        s_KeyList[i].long_press_start_tick  = FLEX_MS_TO_SCAN_CNT(LongTime);
        s_KeyList[i].long_hold_start_tick   = FLEX_MS_TO_SCAN_CNT(LongHoldTime);
        flex_button_register(&s_KeyList[i]);
    }
	#endif
}



/**
 * \brief 轮询函数，通过查询方式检测按键操作
 */
void Key_AnaylePoll(void)
{
    static uint32_t tick = 0;
    if (Hal_SysTick_Get() - tick > (1000 / 100))
    {
        tick = Hal_SysTick_Get();
        flex_button_scan();
    }
}

/**
 * \}
 */
